package com.yanqu.road.dao.impl.activity.cat;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.cat.config.CatLottery;
import com.yanqu.road.entity.activity.cat.config.CatMissionInfo;
import com.yanqu.road.entity.activity.cat.config.CatNpcInfo;
import com.yanqu.road.entity.activity.cat.config.CatSword;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class CatConfigDaoImpl extends TempDao {

    public List<CatMissionInfo> getCatMissionInfoList(int activityId) {
        String getCatMissionInfoSql = "select * from `t_s_activity_cat_mission` where `activityId`=? ORDER BY `id` ASC";
        List<CatMissionInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getCatMissionInfoSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CatMissionInfo catMissionInfo = new CatMissionInfo();
                catMissionInfo.setActivityId(rs.getInt("activityId"));
                catMissionInfo.setId(rs.getInt("id"));
                catMissionInfo.setChapterId(rs.getInt("chapterId"));
                catMissionInfo.setType(rs.getInt("type"));
                catMissionInfo.setEnergyCost(rs.getInt("energyCost"));
                catMissionInfo.setIsBoss(rs.getInt("isBoss"));
                catMissionInfo.setMap(rs.getInt("map"));
                catMissionInfo.setChapterName(rs.getString("chapterName"));
                catMissionInfo.setChapterDesc(rs.getString("chapterDesc"));
                catMissionInfo.setName(rs.getString("name"));
                catMissionInfo.setTerrain(rs.getString("terrain"));
                catMissionInfo.setJailColor(rs.getString("jailColor"));
                catMissionInfo.setRecommendPower(rs.getString("recommendPower"));
                catMissionInfo.setRoundsNumber(rs.getString("roundsNumber"));
                catMissionInfo.setDharma(rs.getString("dharma"));
                catMissionInfo.setDharmaAngerNumber(rs.getString("dharmaAngerNumber"));
                catMissionInfo.setMonsterId(rs.getString("monsterId"));
                catMissionInfo.setMonsterNumber(rs.getString("monsterNumber"));
                catMissionInfo.setBossId(rs.getString("bossId"));
                catMissionInfo.setBossPower(rs.getString("bossPower"));
                catMissionInfo.setRewards(rs.getString("rewards"));
                catMissionInfo.setSuccessRewards(rs.getString("successRewards"));
                catMissionInfo.setSuccessRandomRewards(rs.getString("successRandomRewards"));
                catMissionInfo.setFailRewards(rs.getString("failRewards"));
                catMissionInfo.setInitTerrain(rs.getString("initTerrain"));
                list.add(catMissionInfo);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public List<CatNpcInfo> getCatNpcInfoList(int activityId) {
        String getCatNpcInfoSql = "select * from `t_s_activity_cat_npc` where `activityId`=? ORDER BY `id` ASC";
        List<CatNpcInfo> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getCatNpcInfoSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CatNpcInfo catNpcInfo = new CatNpcInfo();
                catNpcInfo.setActivityId(rs.getInt("activityId"));
                catNpcInfo.setId(rs.getInt("id"));
                catNpcInfo.setIcon(rs.getInt("icon"));
                catNpcInfo.setName(rs.getString("name"));
                catNpcInfo.setDesc(rs.getString("desc"));
                catNpcInfo.setSkillType(rs.getInt("skillType"));
                catNpcInfo.setSkillParameter(rs.getString("skillParameter"));
                list.add(catNpcInfo);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public List<CatLottery> getCatLotteryList(int activityId) {
        String getCatLotterySql = "select * from `t_s_activity_cat_lottery` where `activityId`=? ORDER BY `id` ASC";
        List<CatLottery> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getCatLotterySql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CatLottery catLottery = new CatLottery();
                catLottery.setActivityId(rs.getInt("activityId"));
                catLottery.setId(rs.getInt("id"));
                catLottery.setName(rs.getString("name"));
                catLottery.setWeights(rs.getString("weights"));
                catLottery.setRewards(rs.getString("rewards"));
                catLottery.setIsBroadcast(rs.getInt("isBroadcast"));
                catLottery.setSort(rs.getInt("sort"));
                list.add(catLottery);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }


    public List<CatSword> getCatSwordList(int activityId) {
        String getCatSwordSql = "select * from `t_s_activity_cat_sword` where `activityId`=? ORDER BY `level` ASC";
        List<CatSword> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getCatSwordSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CatSword catSword = new CatSword();
                catSword.setActivityId(rs.getInt("activityId"));
                catSword.setLevel(rs.getInt("level"));
                catSword.setCost(rs.getInt("cost"));
                catSword.setAtk(rs.getInt("atk"));
                list.add(catSword);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }


}
